package com.yootk.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

/**
 * Servlet与内置对象
 */
@WebServlet("/inner")
public class InnerObjectServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        System.out.println("【InnerObjectServlet.init()】RealPath = " + super.getServletContext().getRealPath("/"));
    }

    @Override   // 进行GET请求的处理
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取application内置对象
        System.out.println("【InnerObjectServlet.doGet()】{GenericServlet}RealPath = " + super.getServletContext().getRealPath("/"));
        System.out.println("【InnerObjectServlet.doGet()】{ServletRequest}RealPath = " + req.getServletContext().getRealPath("/"));
        // 获取session内置对象
        HttpSession session = req.getSession(); // 获取HttpSession对象
        System.out.println("SESSIONID = " + session.getId()); // 获取当前的SessionID
        session.invalidate(); // Session失效
    }

    @Override   // 进行POST请求的处理
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
